﻿namespace Webinator.Impls.WatiNImpl
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using Interfaces;
    using WatiN.Core;

    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "Reviewed. Suppression is OK here.")]
    internal class WatiNLogUtil : LogUtil
    {
        private readonly ICore _core;

        public WatiNLogUtil(ICore core)
            : base(core)
        {
            _core = core;
        }

        public override void LogScreenshot()
        {
            if (!_core.Config.LogScreenshots)
            {
                return;
            }

            try
            {
                ((Browser)_core.GetCore()).CaptureWebPageToFile(string.Format("{0}{1}.jpg", _core.Config.LogDirectory, ScreenshotCounter.ToString("D5")));
                ScreenshotCounter++;
            }
            catch (Exception ex)
            {
                Append("Errors", "Unable to capture screenshot");
                Append("Errors", ex.ToString());
            }
        }

        public override void DisplayMessageInBrowser(string message, int interval = 2500)
        {
            MessagePoster messagePoster = new MessagePoster(_core);
            messagePoster.DisplayMessageInBrowser(message, interval);
        }
    }
}